package com.example.demo.dao;

import com.example.demo.model.News;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface NewsMapper {
    /**
     * 根据主键删除 
     * @param id Integer
     * @return int
     */
    @Delete({
        "delete from news",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    /**
     * 保存
     * @param record News
     * @return int
     */
    @Insert({
        "insert into news (id, title, ",
        "content, date, ",
        "url, image_path, ",
        "type)",
        "values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, ",
        "#{content,jdbcType=VARCHAR}, #{date,jdbcType=datetime}, ",
        "#{url,jdbcType=VARCHAR}, #{imagePath,jdbcType=VARCHAR}, ",
        "#{type,jdbcType=VARCHAR})"
    })
    int insert(News record);

    /**
     * 根据字段是否为空保存 
     * @param record News
     * @return int
     */
    @InsertProvider(type=NewsSqlProvider.class, method="insertSelective")
    int insertSelective(News record);

    /**
     * 根据主键查询 
     * @param id Integer
     * @return News
     */
    @Select({
        "select",
        "id, title, content, date, url, image_path, type",
        "from news",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="date", property="date", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="url", property="url", jdbcType=JdbcType.VARCHAR),
        @Result(column="image_path", property="imagePath", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR)
    })
    News selectByPrimaryKey(Integer id);

    /**
     * 根据字段是否为空更新 
     * @param record News
     * @return int
     */
    @UpdateProvider(type=NewsSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(News record);

    /**
     * 根据主键更新所有字段 
     * @param record News
     * @return int
     */
    @Update({
        "update news",
        "set title = #{title,jdbcType=VARCHAR},",
          "content = #{content,jdbcType=VARCHAR},",
          "date = #{date,jdbcType=datetime},",
          "url = #{url,jdbcType=VARCHAR},",
          "image_path = #{imagePath,jdbcType=VARCHAR},",
          "type = #{type,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(News record);
}